
<?php

class Usuario_c extends CI_Controller {

    public function __construct() {
        parent:: __construct();
        $this->load->model('login_m');
        $this->load->helper('form');
        $this->load->helper('html');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->model('usuario_m');
        $this->load->library('encrypt');
        $this->load->helper(array('form', 'url'));
        $this->load->library('image_lib');
        $this->load->helper('file');
      
    }

    
    public function index() {
        if(empty($this->session->userdata['ses_usuario'])){
            $this->login();
        }else{
            $this->irUsuario();     
        } 
          
    }

    
    public function irUsuario () {
      
        $data['username']=$this->session->userdata['ses_usuario'];
        $data['rol']=$this->session->userdata['rol'];
        $usuario=$data['username'];
        $datosEmpleado= $this->usuario_m->getDatosEmpleado($usuario);
        
        
        $data['nombre']= $datosEmpleado->nombre;
        $data['apellido']=$datosEmpleado->apellido;
        $data['email']=$datosEmpleado->email;
        $data['pag'] = 1;
        $data['tieneFoto']=read_file('./bootstrap/images/fotosPerfiles/'.$usuario.'.jpg');
        $this->load->view('plantilla/portal/head', $data);
        $this->load->view('portal/infoUsuario',$data);
        $this->load->view('plantilla/portal/footer',$data);
    }
    
   public function editarUsuario() {
       
        $data['username']=$this->session->userdata['ses_usuario'];
        $data['rol']=$this->session->userdata['rol'];
        $usuario=$data['username'];
        $datosEmpleado= $this->usuario_m->getDatosEmpleado($usuario);
        
        
        $data['nombre']= $datosEmpleado->nombre;
        $data['apellido']=$datosEmpleado->apellido;
        $data['email']=$datosEmpleado->email;
        $data['pag'] = 1;
                
        $this->load->view('plantilla/portal/head', $data);
        $this->load->view('portal/cambioContrasenia',$data);
        $this->load->view('plantilla/portal/footer',$data);
                  
    }
    public function cambiarFoto() {
        $data['username']=$this->session->userdata['ses_usuario'];
        $data['rol']=$this->session->userdata['rol'];
       $data['pag'] = 1;
       $this->load->view('plantilla/portal/head', $data);
       $this->load->view('portal/upload_form', array('error' => ' ' ));
       $this->load->view('plantilla/portal/footer',$data);
        
    }
    
    public function do_upload() {
        $config['upload_path'] = './bootstrap/images/fotosPerfiles/';
        $config['allowed_types'] = 'jpg|png';
        $config['max_size'] = '0';
        $config['max_width'] = '0';
        $config['max_height'] = '0';
        $config['file_name'] = $this->session->userdata['ses_usuario'] . '.jpg';
        $config['overwrite'] = True;
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload()) {
            $data['username']=$this->session->userdata['ses_usuario'];
            $data['rol']=$this->session->userdata['rol'];
            $data['pag'] = 1;
            $data['error'] =  $this->upload->display_errors();
            $this->load->view('plantilla/portal/head', $data);
            $this->load->view('portal/upload_form', $data);
            $this->load->view('plantilla/portal/footer', $data);
        }else{
           //REALIZAMOS RESIZE DE IMAGEN
            $config_img = array();
            $config_img['image_library'] = 'gd2';
            $config_img['source_image'] = './bootstrap/images/fotosPerfiles/' . $this->session->userdata['ses_usuario'] . '.jpg';
            $config_img['create_thumb'] = FALSE;
            $config_img['maintain_ratio'] = FALSE;
            $config_img['width'] = 150;
            $config_img['height'] = 200;
            $this->image_lib->initialize($config_img);
            $this->image_lib->resize();
            $this->irUsuario(); 
        }
    }
        
 
   public function cambiarContrasenia() {
       
       
       $data['username']=$this->session->userdata['ses_usuario'];
       $data['rol']=$this->session->userdata['rol'];
     
       $usuario=$data['username'];
       $datosEmpleado= $this->usuario_m->getDatosEmpleado($usuario);
        
        
       $data['nombre']= $datosEmpleado->nombre;
       $data['apellido']=$datosEmpleado->apellido;
       $data['email']=$datosEmpleado->email;
       $data['pag'] = 1;
             
      
       if (!empty($_POST)) {
           
           $this->load->view('plantilla/portal/head', $data);
           $this->load->view('portal/cambioContrasenia',$data);
           $this->load->view('plantilla/portal/footer',$data);
       }
        
       /*
       $this->form_validation->set_rules('passActual', 'ContraseniaActual', 'required|min_lenght[2]|max_lenght[20]');
       $this->form_validation->set_rules('password', 'Password', 'required', 'required|min_lenght[2]|max_lenght[20]');
       $this->form_validation->set_rules('password2', 'ContraseniaActual', 'required|min_lenght[2]|max_lenght[20]');
       
       if ($this->form_validation->run() == FALSE) {
        *
                    $this->load->view('plantilla/portal/head', $data);
                    $this->load->view('portal/cambioContrasenia',$data);
                    $this->load->view('plantilla/portal/footer',$data);
                } else {
        *            
        *           $isValidLogin = $this->login_m->getLogin($_POST['username'], $this->encrypt->sha1($_POST['password']));
                    if (!$isValidLogin) {
       */
       
      $nuevacontrasenia = $this->encrypt->sha1($_POST['password']);   
      $this->usuario_m->setContrasenia($usuario,$nuevacontrasenia);
      }
}

?>